﻿
@{
    ViewData["Title"] = "Default";
}

<script type="text/javascript">
    if ('@ViewBag.Resubmit'.toLowerCase() == 'false' && '@(SessionUtility.GetBasicUserInfo().ThirdDeptCode)' != "") {
        $('#@ViewData["firstDeptId"]').val('@(SessionUtility.GetBasicUserInfo().FirstDeptCode)');
        setSecondDept();
        $('#@ViewData["secondDeptId"]').val('@(SessionUtility.GetBasicUserInfo().SecondDeptCode)');
        setThirdDept();
        $('#@ViewData["thirdDeptId"]').val('@(SessionUtility.GetBasicUserInfo().ThirdDeptCode)');
    } else {
        $('#@ViewData["firstDeptId"]').val('@(Model.FirstDeptCode)');
        setSecondDept();
        $('#@ViewData["secondDeptId"]').val('@(Model.SecondDeptCode)');
        setThirdDept();
        $('#@ViewData["thirdDeptId"]').val('@(Model.ThirdDeptCode)');
    }

    function setSecondDept() {
        $('#@ViewData["secondDeptId"]').empty();
        var dept1Code = $('#@ViewData["firstDeptId"]').val();
        $.ajax({
            url: "/Sys/Sys_Department/GetDept2",
            data: "dept1Code=" + dept1Code + "&hasExtra=false&isHistory=0",
            method: "get",
            async: false,
            dataType: "json",
            success: function (data) {
                $.each(data, function (i, item) {
                    $("<option></option>")
                        .val(item["Dept2Code"])
                        .text(item["Dept2Name"])
                        .appendTo($('#@ViewData["secondDeptId"]'));
                });
                setThirdDept();
            },
            error: function (err) { }
        });
    }

    function setThirdDept() {
        $('#@ViewData["thirdDeptId"]').empty();
        var dept2Code = $('#@ViewData["secondDeptId"]').val();
        $.ajax({
            url: "/Sys/Sys_Department/GetDept3",
            data: "dept2Code=" + dept2Code + "&hasExtra=false&isHistory=0",
            method: "get",
            async: false,
            dataType: "json",
            success: function (data) {
                $.each(data, function (i, item) {
                    $("<option></option>")
                        .val(item["Dept3Code"])
                        .text(item["Dept3Name"])
                        .appendTo($('#@ViewData["thirdDeptId"]'));
                });
                if ('@ViewData["handleDeptManager"]'.toLowerCase() == 'true') {
                    setManager();
                }
            },
            error: function (err) { }
        });
    }

    function setManager() {
        var dept1Code = $('#@ViewData["firstDeptId"]').val();
        var dept2Code = $('#@ViewData["secondDeptId"]').val();
        var dept3Code = $('#@ViewData["thirdDeptId"]').val();
        if (dept1Code == "-1" || dept2Code == "-1" || dept3Code == "-1") {
            return;
        }
        $.ajax({
            url: "/Sys/Sys_Department/GetManagerAndId",
            data: "dept1Code=" + dept1Code + "&dept2Code=" + dept2Code + "&dept3Code=" + dept3Code,
            method: "get",
            async: false,
            dataType: "json",
            success: function (data) {
                $('#@ViewData["deptManagerId"]').val(data.DeptManager);
                $('#@ViewData["deptId"]').val(data.DeptId);
            },
            error: function (err) { }
        });
    }
</script>